package b4.view.component.smithy
{
	import b4.model.vo.player.Obj;
	import b4.view.component.events.ItemClickEvent;
	import b4.view.component.pack.BasicObjItem;
	
	import flash.events.MouseEvent;
	
	import org.aswing.ASColor;
	import org.aswing.AssetPane;
	import org.aswing.JLabel;
	import org.aswing.border.LineBorder;
	
	public class BoxObj extends AssetPane
	{
		public static const STATUS_OCCUPIED:int = 1;
		public static const STATUS_OUT_RANGE:int = 2;
		public static const STATUS_SUCCESS :int = 0;
		public var maxIdx:int;
		private var minIdx:int;
		private var boxName:String;
		public var hasObj:Boolean;
		private var boxText:JLabel;
		public var boxItem:BasicObjItem;
		
		public function BoxObj(name:String,  minIndex:int, maxIndex:int )
		{
			super();
			boxText= new JLabel(name);
			boxText.setSizeWH(40, 40);
			maxIdx = maxIndex;
			minIdx = minIndex;
			this.hasObj = false;
			this.setAsset(boxText);
			addEventListener(MouseEvent.CLICK, clickItem);
			setBorder(new LineBorder(null, ASColor.BLUE));
		}
		
		public function loadObj(objItem:BasicObjItem, enforce:Boolean = false):int
		{
			if (!enforce && (objItem.obj.index > this.maxIdx || objItem.obj.index < this.minIdx))
			{
				return STATUS_OUT_RANGE;
			}
			else if (!enforce && this.hasObj == true)
			{
				return STATUS_OCCUPIED;
			}
			else
			{	
				this.setAsset(objItem);
				this.boxItem = objItem;
				this.hasObj	= true;
				return STATUS_SUCCESS;
			}
		}
		
		private function clickItem(e:MouseEvent):void
		{
			if (hasObj != false)
			{
				loadOffItem();
				var itemBox:BoxObj = e.currentTarget as BoxObj;
				var loadOffEvent:ItemClickEvent = new ItemClickEvent(ItemClickEvent.LOAD_ITEM_OFF);
				loadOffEvent.data = itemBox.boxItem.obj;
				dispatchEvent(loadOffEvent);
			}
		}
		
		public function loadOffItem():void
		{
			this.hasObj = false;
			this.setAsset(boxText);	
		}
	}
	
}
